package newsmessages.observer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: jeroen
 * Date: 4/8/13
 * Time: 2:22 PM
 */
public class Subject implements ISubject, Runnable {
    protected ArrayList<IObserver> observers = new ArrayList<IObserver>();


    @Override
    public void run() {
        try {
            final InputStreamReader isr = new InputStreamReader(System.in);
            final BufferedReader br = new BufferedReader(isr);
            while (true) {
                String response = br.readLine();
                if (response.equals("hoi")) {
                    notifyObservers();
                }
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void notifyObservers() {
        for (IObserver IObserver : this.observers) {
            IObserver.notifyNewMessage();
        }
    }

    @Override
    public void attach(IObserver IObserver) {
        this.observers.add(IObserver);
    }

    @Override
    public void detach(IObserver IObserver) {
        this.observers.remove(IObserver);
    }
}